package com.easyOrder.core.push;

import com.easyOrder.core.utils.Base64;
import com.easyOrder.core.exception.api.HttpClientException;
import com.easyOrder.core.utils.HttpObjectRequestClient;
import org.apache.log4j.Logger;

import java.util.HashMap;
import java.util.Map;

public class JpushDelivery implements PushDelivery {
	
	private static final String URI = "https://api.jpush.cn/v3/push";

	private String appKey;

	private String masterSecret;

	private String authorization;
	
	private static final Logger logger = Logger.getLogger(JpushDelivery.class);

	public String getAppKey() {
		return appKey;
	}

	public void setAppKey(String value) {
		appKey = value;
	}

	public String getMasterSecret() {
		return masterSecret;
	}

	public void setMasterSecret(String value) {
		masterSecret = value;
	}

	public void send(Notification message) {
		ensureAuthorization();
		String[] deviceCodes = new String[message.getRecipients().size()];
		for (int i = 0; i < message.getRecipients().size(); i++) {
			deviceCodes[i] = message.getRecipients().get(i).getCode();
		}

		JpushMessage jpushMessage = new JpushMessage();
		jpushMessage.getAudience().setRegistration_id(deviceCodes);
		jpushMessage.getNotification().getAndroid()
				.setTitle(message.getTitle());
		jpushMessage.getNotification().setAlert(message.getContent());

		HttpObjectRequestClient httpClient = new HttpObjectRequestClient();
		httpClient.setToken("Authorization=Basic " + authorization);
		Object reponse = null;
		try {
			reponse = httpClient.execute(URI, "POST", jpushMessage);
		} catch (HttpClientException e) {
			logger.error(e, e);
		}
		if ((reponse != null) && ((Map<?, ?>) reponse).get("msg_id") == null) {
			logger.error("推送 Jpush 消息失败");
		}
	}

	private void ensureAuthorization() {
		authorization = Base64.encode(appKey + ":" + masterSecret, false);
	}

	class JpushMessage {
		private String platform = "all";

		private JpushMessageNotification notification;

		private JpushMessageAudience audience;

		public JpushMessage() {
			notification = new JpushMessageNotification();
			audience = new JpushMessageAudience();
		}

		public String getPlatform() {
			return platform;
		}

		public void setPlatform(String platform) {
			this.platform = platform;
		}

		public JpushMessageNotification getNotification() {
			return notification;
		}

		public void setNotification(JpushMessageNotification notification) {
			this.notification = notification;
		}

		public JpushMessageAudience getAudience() {
			return audience;
		}

		public void setAudience(JpushMessageAudience audience) {
			this.audience = audience;
		}
	}

	class JpushMessageNotification {
		private String alert;

		private JpushMessageNotificationAndroid android;

		public JpushMessageNotification() {
			android = new JpushMessageNotificationAndroid();
		}

		public String getAlert() {
			return alert;
		}

		public void setAlert(String alert) {
			this.alert = alert;
		}

		public JpushMessageNotificationAndroid getAndroid() {
			return android;
		}

		public void setAndroid(JpushMessageNotificationAndroid android) {
			this.android = android;
		}
	}

	class JpushMessageNotificationAndroid {
		private String title;

		private Map<Object, Object> extras;

		public JpushMessageNotificationAndroid() {
			extras = new HashMap<Object, Object>();
		}

		public Map<?, ?> getExtras() {
			return extras;
		}

		public void setExtras(Map<Object, Object> extras) {
			this.extras = extras;
		}

		public String getTitle() {
			return title;
		}

		public void setTitle(String title) {
			this.title = title;
		}
	}

	class JpushMessageAudience {
		private String[] registration_id;

		public String[] getRegistration_id() {
			return registration_id;
		}

		public void setRegistration_id(String[] registration_id) {
			this.registration_id = registration_id;
		}
	}
}
